package com.zc.trendsDispatch.service.impl;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zc.trendsDispatch.domain.ChargeStationPrediction;
import com.zc.trendsDispatch.mapper.ChargeStationPredictionMapper;
import com.zc.trendsDispatch.service.IChargeStationPredictionService;
import org.apache.commons.collections4.Put;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 场站功率预测Service业务层处理
 *
 * @author glb
 * @date 2024-02-21
 */
@Service
public class ChargeStationPredictionServiceImpl implements IChargeStationPredictionService
{
    @Autowired
    private ChargeStationPredictionMapper chargeStationPredictionMapper;

    /**
     * 查询场站功率预测
     *
     * @param id 场站功率预测主键
     * @return 场站功率预测
     */
    @Override
    public ChargeStationPrediction selectChargeStationPredictionById(Long id)
    {
        return chargeStationPredictionMapper.selectChargeStationPredictionById(id);
    }

    @Override
    public List<Map> getStationInfo(ChargeStationPrediction chargeStationPrediction) {
        List<ChargeStationPrediction> chargeStationPredictions = chargeStationPredictionMapper.selectChargeStationPredictionList(chargeStationPrediction);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd ");
            if(chargeStationPredictions.size()>0){
                ChargeStationPrediction chargeStationPrediction1 = chargeStationPredictions.get(0);
                //预测值
                String forecastValue = chargeStationPrediction1.getForecastValue();
                Map  assemble = JSONObject.parseObject(forecastValue);
                Map objectMapKey = sortMapByKey(assemble);
                List<Map> maps=new ArrayList<>();
                objectMapKey.forEach((k,y)->{
                    Map inDetail = JSONObject.parseObject(String.valueOf(y));
                    inDetail.put("time",dateFormat.format(chargeStationPrediction1.getForecastDate())+inDetail.get("time"));
                    inDetail.put("stationName",chargeStationPrediction1.getStationName());
                    maps.add(inDetail);
                });
                return maps;
            }
        return null;
    }

    public static Map<String,Object> sortMapByKey(Map<String,Object> map){
        List<Map.Entry<String,Object>> entryList = new ArrayList<>(map.entrySet());
        entryList.sort(Comparator.comparing(o-> o.getKey()));
        //遍历排序好的list，一定要放进LinkedHashMap，因为只有LinkedHashMap是根据插入顺序进行存储
        LinkedHashMap<String,Object> linkedHashMap = new LinkedHashMap<>();
        for(Map.Entry<String,Object> entry : entryList){
            linkedHashMap.put(entry.getKey(),entry.getValue());
        }
        return linkedHashMap;
    }


    /**
     * 查询场站功率预测列表
     *
     * @param chargeStationPrediction 场站功率预测
     * @return 场站功率预测
     */
    @Override
    public List<ChargeStationPrediction> selectChargeStationPredictionList(ChargeStationPrediction chargeStationPrediction)
    {
        return chargeStationPredictionMapper.selectChargeStationPredictionList(chargeStationPrediction);
    }

    /**
     * 新增场站功率预测
     *
     * @param chargeStationPrediction 场站功率预测
     * @return 结果
     */
    @Override
    public int insertChargeStationPrediction(ChargeStationPrediction chargeStationPrediction)
    {
        return chargeStationPredictionMapper.insertChargeStationPrediction(chargeStationPrediction);
    }

    /**
     * 修改场站功率预测
     *
     * @param chargeStationPrediction 场站功率预测
     * @return 结果
     */
    @Override
    public int updateChargeStationPrediction(ChargeStationPrediction chargeStationPrediction)
    {
        return chargeStationPredictionMapper.updateChargeStationPrediction(chargeStationPrediction);
    }

    /**
     * 批量删除场站功率预测
     *
     * @param ids 需要删除的场站功率预测主键
     * @return 结果
     */
    @Override
    public int deleteChargeStationPredictionByIds(Long[] ids)
    {
        return chargeStationPredictionMapper.deleteChargeStationPredictionByIds(ids);
    }

    /**
     * 删除场站功率预测信息
     *
     * @param id 场站功率预测主键
     * @return 结果
     */
    @Override
    public int deleteChargeStationPredictionById(Long id)
    {
        return chargeStationPredictionMapper.deleteChargeStationPredictionById(id);
    }

    @Override
    public int insertChargeStationPredictionTest(ChargeStationPrediction chargeStationPrediction) {
        String value="";

        return 0;
    }
}
